package com.apache.security.util;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Vector;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

import com.apache.security.util.XssUtil.XssFilterTypeEnum;

public class SecurityHttpServletRequest extends HttpServletRequestWrapper {

	public SecurityHttpServletRequest(HttpServletRequest request) {
		super(request);
	}

	public String getParameter(String name) {
		return XssUtil.xssFilter(super.getParameter(name), null);
	}

	public Map<String, String[]> getParameterMap() {
		Map<String, String[]> paramsMap = super.getParameterMap();
		if (paramsMap == null || paramsMap.isEmpty()) {
			return paramsMap;
		}
		Map<String, String[]> resParamsMap = new HashMap<String, String[]>();
		Iterator<Entry<String, String[]>> iter = paramsMap.entrySet().iterator();
		while (iter.hasNext()) {
			Entry<String, String[]> entry = iter.next();
			resParamsMap.put((entry.getKey()), filterList(entry.getValue()));
		}
		return resParamsMap;
	}

	public Enumeration<String> getParameterNames() {
		Enumeration<String> enums = super.getParameterNames();
		Vector<String> vec = new Vector<String>();
		while (enums.hasMoreElements()) {
			String value = enums.nextElement();
			vec.add(XssUtil.xssFilter(value, null));
		}
		return vec.elements();
	}

	public String[] getParameterValues(String name) {
		return filterList(super.getParameterValues(name));
	}

	private String[] filterList(String[] value) {
		if (value == null || value.length == 0) {
			return value;
		}
		List<String> resValueList = new ArrayList<String>();
		for (String val : value) {
			resValueList.add(XssUtil.xssFilter(val, null));
		}
		return resValueList.toArray(new String[resValueList.size()]);
	}

}
